﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.BussLogic;
using TimKen.DataEntity;
using System.Data;
using System.Text;
using TimKen.App;

namespace TimKen.Web
{
    public partial class ChildCategory : App.WebPage
    {
        public PCategoryManager pcm = new PCategoryManager();

        public string parentLink;

        public int categoryID = 0;
        public int brandid = 0;
        public string categoryName;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                categoryID = 0;
            }
            else
            {
                categoryID = int.Parse(Request.QueryString["cid"]);
                PCategory category = pcm.GetModel(categoryID, (LanguageEnum)LanguageID);

                if (category.SortOrder == 2)
                    Response.Redirect("ChildCategoryList.aspx?" + Request.QueryString);
                if (category.SortOrder == 3)
                    Response.Redirect("ChildCategoryTabs.aspx?" + Request.QueryString);
                if (category.SortOrder == 4)
                    Response.Redirect("ChildCategoryGrid.aspx?" + Request.QueryString);

                categoryName = category.CategoryName;
            }

            if (string.IsNullOrEmpty(Request.QueryString["brandid"]) || !TypeParse.IsNumeric(Request.QueryString["brandid"]))
            {
                brandid = 0;
            }
            else
                brandid = int.Parse(Request.QueryString["brandid"]);


            DataSet ds = pcm.GetChild(categoryID, LanguageID, brandid);


            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Redirect("POptionListSearch.aspx?CategoryID=" + categoryID + "&brandid=" + brandid);
            }
            else
            {
                ProductManager pm = new ProductManager();
                divdh.InnerHtml = GetCategoryListSite(categoryID, brandid);
                dlCategory.DataSource = ds;
                dlCategory.DataBind();
            }

            parentLink = GetParentLink(pcm.GetAllParent(categoryID, LanguageID));

        }

        private string GetParentLink(string linkData)
        {
            string[] links = linkData.Split(',');
            StringBuilder sb = new StringBuilder();
            if (links.Length > 0)
            {
                sb.Append("<a href=\"Category.aspx?\">" + GetLangString("ProductCategories") + "</a>&nbsp;—&nbsp;");


                for (int i = 0; i < links.Length; i++)
                {
                    string link = links[i];
                    string[] nameid = link.Split(':');

                    if (nameid.Length == 2 && i != links.Length - 1)
                    {
                        sb.Append("<a href=\"ChildCategory.aspx?cid=" + nameid[1] + "&brandid=" + brandid + "\">" + nameid[0] + "</a>&nbsp;—&nbsp;");
                    }
                    else
                    {
                        sb.Append(nameid[0] + "&nbsp;—&nbsp;");
                    }
                }
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 7, 7);
            }
            return sb.ToString();
        }
        public string returnName(string strName)
        {
            if (LanguageID == 1) //中文
            {
                if (strName.Length > 19)
                {
                    return strName.Substring(0, 18) + "...";
                }
                else
                {
                    return strName;
                }
            }
            else//英文 
            {
                if (strName.Length > 33)
                {
                    return strName.Substring(0, 30) + "...";
                }
                else
                {
                    return strName;
                }
            }
        }
    }
}